package com.thinkgem.jeesite.modules.withdraw;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.utils.HttpClientUtil;
import com.thinkgem.jeesite.common.utils.PropertiesLoader;
import com.thinkgem.jeesite.modules.withdraw.entity.PayCashWithdraw;
import com.thinkgem.jeesite.modules.withdraw.service.PayCashWithdrawService;
import org.apache.commons.collections.map.LinkedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2017/8/8.
 */
@Service
public class WithdrawServiceImpl implements WithdrawService{

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private PayCashWithdrawService payCashWithdrawService;

	@Override
	@Transactional
	public Map<String, Object> dealWithdraw(PayCashWithdraw payCashWithdraw) {
		Map<String, Object> resMap  = new HashMap<String, Object>();
		if (1 == payCashWithdraw.getCheckStatus()){
			//调取三方 提现接口
			resMap  = withdraw(payCashWithdraw);
			logger.info("调取三方提现接口返回信息："+ JSONObject.toJSONString(resMap));
		}
		return resMap;
	}

	/**
	 * 调用提现接口
	 * @param payCashWithdraw
	 * @return
	 */
	private Map<String, Object> withdraw(PayCashWithdraw payCashWithdraw) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String ,String> map = new LinkedHashMap<String ,String>();
		map.put("amount",payCashWithdraw.getAmount());
		map.put("userId",String.valueOf(payCashWithdraw.getUserId()));
		map.put("orderId",payCashWithdraw.getOrderId());
		try {
			//String url = "http://192.168.1.104:8088/fywithdraw/withdraw"; //本地
			//String url = "http://118.190.78.113:8088/fywithdraw/withdraw";//测试
			//String url = "https://www.hongjins.com/fywithdraw/withdraw";//生产
			PropertiesLoader propertiesLoader = new PropertiesLoader("withdraw.properties");
			String url = propertiesLoader.getProperty("withdraw.url");
			String resultData = HttpClientUtil.doGet(url, map);
			resMap = new JSONObject().parseObject(resultData, Map.class);//将json字符串转换为json对象
		} catch (Exception e) {
			logger.error("调用提现接口失败",e);
		}
		return  resMap;
	}
}
